package org.example.demo4.controller.freight;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.mybatisflex.core.paginate.Page;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.demo4.annotation.Log;
import org.example.demo4.utils.PageDomain;
import org.example.demo4.utils.TableSupport;
import org.example.demo4.utils.enums.BusinessType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.example.demo4.service.FreightAddressService;
import org.example.demo4.model.FreightAddressEntity;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;

/**
 * 控制层。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@RestController
@RequestMapping("/freightAddress")
@Tag(name  = "运费地址")

public class FreightAddressController {
    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FreightAddressService freightAddressService;


    /**
     * 添加
     *
     * @param freightAddress
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("/save")
    @Operation(summary = "保存")
    @ApiOperationSupport(order = 4)
    @Log(title = "新增", businessType = BusinessType.INSERT)
    public boolean save(@RequestBody FreightAddressEntity freightAddress) {
        return freightAddressService.save(freightAddress);
    }


    /**
     * 根据主键删除
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @PostMapping("/remove/{id}")
    @Operation(summary = "删除")
    @ApiOperationSupport(order = 6)
    @Log(title = "删除", businessType = BusinessType.DELETE)
    public boolean remove(@PathVariable Serializable id) {
        return freightAddressService.removeById(id);
    }


    /**
     * 根据主键更新
     *
     * @param freightAddress
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("/update")
    @Operation(summary = "更新")
    @ApiOperationSupport(order = 5)
    public boolean update(@RequestBody FreightAddressEntity freightAddress) {
        return freightAddressService.updateById(freightAddress);
    }


    /**
     * 查询所有
     *
     * @return 所有数据
     */
    @GetMapping("/list")
    @Operation(summary = "查询")
    @ApiOperationSupport(order = 1)
    @Log(title = "查询", businessType = BusinessType.QUERY)
    public Page<FreightAddressEntity> list(FreightAddressEntity param) {
        Page<FreightAddressEntity> page = new Page<FreightAddressEntity>();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        page.setPageSize(pageDomain.getPageSize().longValue());
        page.setPageNumber(pageDomain.getPageNum());
        return freightAddressService.page(page);
//        return freightAddressService.list();
    }


    /**
     * 根据主键获取详细信息。
     *
     * @param id freightAddress主键
     * @return 详情
     */
    @GetMapping("/getInfo/{id}")
    @Operation(summary = "获取明细")
    @ApiOperationSupport(order = 3)
    public FreightAddressEntity getInfo(@PathVariable Serializable id) {
        return freightAddressService.getById(id);
    }


    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("/page")
    @Operation(summary = "分页")
    @ApiOperationSupport(order = 2)
    public Page<FreightAddressEntity> page(Page<FreightAddressEntity> page) {
        return freightAddressService.page(page);
    }
}